<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<link rel="stylesheet" media="screen" type="text/css" href="/public/css/admin/category/style.css" />
		<link rel="stylesheet" href="/public/css/admin/category/nyroModal-1.3.0/styles/nyroModal.css" type="text/css" media="screen" />

		<script type="text/javascript" src="/public/js/jquery.simple.tree.js"></script>
		<script src="/public/css/admin/category/nyroModal-1.3.0/js/jquery.nyroModal-1.3.0.pack.js"></script> 
		<script type="text/javascript" src="/public/js/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">    
		jQuery.noConflict();
		var simpleTreeCollection;	
		$(document).ready(function(){ 
			
			simpleTreeCollection = $('.simpleTree').simpleTree({
				
				autoclose: false,
				docToFolderConvert: false,
				
				afterClick:function(node){ 
					//// nothing to do for now...
					categoryId = $('span:first',node).parent("li").attr("id");
					categoryId = $('span:first',node).parent("li").attr("id");
					$.get('http://shoppingcartcnpm.orgfree.com/index.php/Longadmin/category/catdesajax', {id:categoryId}, function(data) {
						$('#textAjax').html(data).hide().fadeIn(1000);
					});
				},
				
				afterDblClick:function(node){
					categoryId = $('span:first',node).parent("li").attr("id");
					parentId = $('span:first',node).parent("li").parent("ul").parent("li").attr("id");
				
					$.nyroModalManual({
						url: '/application/models/ajax/category/update.php?cat_id='+categoryId,
						width: 250, // default Width If null, will be calculate automatically
						height: 130, // default Height If null, will be calculate automatically
						minWidth: null, // Minimum width
						minHeight: null, // Minimum height
						endRemove: function() {window.location.reload()}
					});		
	
					return false;
							
				},			
				afterMove:function(){	
					var serialStr = "";
					var order = "";
					$("ul.simpleTree li span").each(function(){			
						parentId = $(this).parent("li").parent("ul").parent("li").attr("id");
						if (parentId == undefined) parentId = "root";
						order = (($(this).parent("li").prevAll("li").size()+1))/2; 
						if ( parentId != "root") serialStr += ""+parentId+":"+$(this).parent("li").attr("id")+":"+order+"|";
					});
					$.ajax({
					   type: "POST",
					   url: "/application/models/ajax/category/saveData.php",
					   data: "serialized="+serialStr,
					   success: function(msg){
					   	 $("#serializedList").html(msg);
					   }
					 });
			
					return false;
					
				},
				docToFolderConvert: false,
				afterAjax:function()
				{
					//alert('Loaded');
				},
				animate:true
			});	
			
			$(".add_category").click(function(){
				categoryId = $(this).parent("li").attr("id");
				$.nyroModalManual({
					url: '/application/models/ajax/category/add.php?cat_parentID='+categoryId,
					width: 280, // default Width If null, will be calculate automatically
					height: 200, // default Height If null, will be calculate automatically
					minWidth: null, // Minimum width
					minHeight: null, // Minimum height
					resizeable: true,
					endRemove: function() {window.location.reload()}
					
				});
			});
			
			$(".delete_category").click(function(){
				parentId = $(this).parent("li").attr("id");
				category_name = $(this).siblings("span").text();
				$.nyroModalManual({
					url: '/application/models/ajax/category/delete.php?cat_parentID='+parentId+'&cat_ten='+category_name,
					endRemove: function() {window.location.reload()},
					width: 450, // default Width If null, will be calculate automatically
					height: 150, // default Height If null, will be calculate automatically
					minWidth: null, // Minimum width
					minHeight: null, // Minimum height
					resizeable: false, // Indicate if the content is resizable. Will be set to false for swf
					autoSizable: false, // Indicate if the content is auto sizable. If not, the min size will be used
					padding: 0 // padding for the max modal size	
				});
			});			
			
		});
		</script>
		
	</head>
<?php 
		// Cai nay phai de o controller, nhung ma gio chua biet, hix
		$db = Zend_Registry::get('db');
		$table = 'category';
		
		$select = $db->select()->from($table);
		$category = $db->query($select)->fetchAll();
		// chua biet xu ly the nao day, hix, sao lai  bi the nay nhi?  :@
		// Cai nay se cho vao trong ham helper moi dung chuan :D, gio cu the da 
		function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
				
			foreach ($array as $category) {
				
				if ($currentParent == $category['cat_parentID']) {						
					
					if ($currLevel > $prevLevel) echo " <ul> \n"; 
					
					if ($currLevel == $prevLevel) echo " </li> \n";
					
					echo '<li id="'.$category['cat_id'].'"><span>'.$category['cat_ten'].'</span><img src="/public/css/admin/category/images/16-circle-blue-add.png" alt="add subcategory" width="16" height="16" class="add_category"/> <img src="/public/css/admin/category/images/16-circle-red-remove.png" alt="remove" width="16" height="16" class="delete_category"/>';
								
					if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
					
					$currLevel++; 

					createTree ($array, $category['cat_id'], $currLevel, $prevLevel);
					
					$currLevel--;	 		 	

				}	

			}
			
			if ($currLevel == $prevLevel) echo "\n </li> \n </ul> \n";

		}	
?>
	

	<ul class="simpleTree">
		<li id="0" class="root"><span>Categories</span><img src="/public/css/admin/category/images/16-circle-blue-add.png" alt="add subcategory" width="16" height="16" class="add_category"/> <img src="/public/css/admin/category/images/16-circle-red-remove.png" alt="remove" width="16" height="16" class="delete_category"/>
			<?php createTree($category, 0); ?>
		</li>
	</ul>
	<div id= "textAjax" style="display:block; width: 10em ;position: relative; left: 350px; top: -500px"></div>
</html>